//
//  LoginViewController.m
//  MyProject
//
//  Created by Pei on 16/6/23.
//  Copyright © 2016年 Xiaobo Pei. All rights reserved.
//

#import "LoginViewController.h"
#import "YMNetLib.h"
#import "YMNetDataUtil.h"
#import "UserInfo.h"
#import <AdSupport/ASIdentifierManager.h>

@interface LoginViewController ()<UITextFieldDelegate, YMNetLibDelegate, UIScrollViewDelegate>
{
    UIScrollView *_mainScrollView;
    
    UITextField *_currentField;
    
    UIButton *_loginBtn;
    
    UITextField *_nameField;
    UITextField *_pwdField;
    
    NSString *_currentAction;
}

@end

@implementation LoginViewController

- (void)viewDidLoad
{
    self.navTitle = @"用户登录";
    
    [super viewDidLoad];
    
    UIButton *backButton = [[UIButton alloc] initWithFrame:CGRectMake(0, 0, 22, 22)];
    [backButton setBackgroundImage:[UIImage imageNamed:@"01back"] forState:UIControlStateNormal];
    [backButton setBackgroundImage:[UIImage imageNamed:@"01back"] forState:UIControlStateHighlighted];
    [backButton addTarget:self action:@selector(backAction) forControlEvents:UIControlEventTouchUpInside];
    //[self.navigationItem setLeftBarButtonItem:[[UIBarButtonItem alloc] initWithCustomView:backButton]];
    //self.navigationItem.hidesBackButton = YES;
    
    self.view.backgroundColor = kWhiteColor;
    
    _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0.0f, 0.0f, kDeviceWidth, kDeviceHeight-NAV_HEIGHT)];
    _mainScrollView.contentSize = CGSizeMake(kDeviceWidth, kDeviceHeight-NAV_HEIGHT+1.0f);
    _mainScrollView.backgroundColor = kWhiteColor;
    [self.view addSubview:_mainScrollView];
    
    [self setupLayout];
    
}


#pragma mark ************************ UI *****************************************
- (void)setupLayout
{
    UIImageView *headerImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"02logo"]];
    headerImageView.frame = CGRectMake((kDeviceWidth-639.0f/3.0f)/2.0f, 40.0f, 639.0f/3.0f, 428.0f/3.0f);
    [_mainScrollView addSubview:headerImageView];
    
    UIView *topLine = [[UIView alloc] initWithFrame:CGRectMake((kDeviceWidth-639.0f/3.0f)/2.0f, headerImageView.bottom+20.0f, 639.0f/3.0f, 0.5f)];
    topLine.backgroundColor = kGreenColor;
    [_mainScrollView addSubview:topLine];
    
    UIView *centerLine = [[UIView alloc] initWithFrame:CGRectMake((kDeviceWidth-639.0f/3.0f)/2.0f, topLine.bottom+40.0f, 639.0f/3.0f, 0.5f)];
    centerLine.backgroundColor = kGreenColor;
    [_mainScrollView addSubview:centerLine];
    
    UIView *bottomLine = [[UIView alloc] initWithFrame:CGRectMake((kDeviceWidth-639.0f/3.0f)/2.0f, centerLine.bottom+40.0f, 639.0f/3.0f, 0.5f)];
    bottomLine.backgroundColor = kGreenColor;
    [_mainScrollView addSubview:bottomLine];
    
    UIImageView *nameImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"04yonghuming"]];
    nameImageView.frame = CGRectMake(topLine.origin.x, topLine.bottom+(40-40/2)/2.0f, 27.0f/2.0f, 40.0f/2.0f);
    [_mainScrollView addSubview:nameImageView];
    
    UIImageView *passwordImageView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"05mima"]];
    passwordImageView.frame = CGRectMake(topLine.origin.x, centerLine.bottom+(40-40/2)/2.0f, 27.0f/2.0f, 40.0f/2.0f);
    [_mainScrollView addSubview:passwordImageView];
    
    
    
    _nameField = [[UITextField alloc] init];
    _nameField.frame = CGRectMake(nameImageView.right+10.0f, topLine.bottom+(40-Home_Big_Size)/2.0f, headerImageView.width-10-40/2, Home_Big_Size+2.0f);
    _nameField.attributedPlaceholder =[[NSAttributedString alloc] initWithString:@"用户名" attributes:@{NSForegroundColorAttributeName: kGreenColor, NSFontAttributeName : kAppFont(Home_Big_Size)}];
    _nameField.delegate = self;
    _nameField.tag = 116;
    //_nameField.text = @"pdtest02";//@"admin";
    _nameField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [_mainScrollView addSubview:_nameField];
    
    //test003 123456
    
    //admin 123456
    
    //testuser006 123456
    
    _pwdField = [[UITextField alloc] init];
    _pwdField.frame = CGRectMake(passwordImageView.right+10.0f, centerLine.bottom+(40-Home_Big_Size)/2.0f, headerImageView.width-10-40/2, Home_Big_Size+2.0f);
    _pwdField.attributedPlaceholder =[[NSAttributedString alloc] initWithString:@"5-12位密码" attributes:@{NSForegroundColorAttributeName: kGreenColor, NSFontAttributeName : kAppFont(Home_Big_Size)}];
    _pwdField.delegate = self;
    _pwdField.secureTextEntry = YES;
    _pwdField.tag = 118;
    //_pwdField.text = @"123456";
    _pwdField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [_mainScrollView addSubview:_pwdField];
    
    _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _loginBtn.frame = CGRectMake(headerImageView.origin.x, bottomLine.bottom+20.0f, headerImageView.width, 40.0f);
    [_loginBtn setBackgroundColor:kBtnBackgroundColor];
    [_loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [_loginBtn addTarget:self action:@selector(loginBtnAction) forControlEvents:UIControlEventTouchUpInside];
    _loginBtn.layer.masksToBounds = YES;
    _loginBtn.layer.cornerRadius = 4.0f;
    [_mainScrollView addSubview:_loginBtn];
    
}
#pragma mark ************************ Action ***********************************
- (void)backAction
{
    [kAPPDelegate rootView];
}

- (void)loginBtnAction
{
    NSString *nameStr = _nameField.text;
    if (nameStr == nil || nameStr.length <= 0){
        [[YMUIUtil getInstance] showTipViewAutoDis:@"请输入用户名"];
        return;
    }

    NSString *pwdStr = _pwdField.text;
    if (pwdStr == nil || pwdStr.length <= 4 || pwdStr.length > 12){
        [[YMUIUtil getInstance] showTipViewAutoDis:@"请输入正确的密码"];
        return;
    }

    NSString *macStr = [[NSUserDefaults standardUserDefaults] valueForKey:@"JPUSHServiceRegistrationID"];
    
    NSLog(@"macStrmacStrmacStr++++++%@",macStr);
    
    //NSDictionary *bodyDic = [NSDictionary dictionaryWithObjectsAndKeys:_nameField.text,@"UserUID",_pwdField.text,@"Password",[[[ASIdentifierManager sharedManager] advertisingIdentifier] UUIDString], @"DeviceMAC", nil];
    NSDictionary *bodyDic = [NSDictionary dictionaryWithObjectsAndKeys:_nameField.text,@"UserUID",_pwdField.text,@"Password", macStr, @"DeviceMAC", nil];
    [[YMNetLib getInstance] connectWithAction:Login_API method:@"POST" withContent:nil withBody:bodyDic withDelegate:self];
    _currentAction = Login_API;
}


- (void)NetDoOverSuccess:(NSDictionary*)body
{
    if ([_currentAction isEqualToString:Login_API]) {
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1)
        {
            
            [[UserInfo getInstance] setUserDic:body];
            
            [[NSUserDefaults standardUserDefaults] setBool:YES forKey:Login_Status];
            
            [[YMNetLib getInstance] connectWithAction:User_Privilege method:@"GET" withContent:[NSString stringWithFormat:@"UserUid=%@", [[UserInfo getInstance] getUserID]] withBody:nil withDelegate:self];
            _currentAction = User_Privilege;
            
        }
        else
        {
            if ([[body objectForKey:@"ErrorCode"] isEqualToString:@"0001"]){
                [[YMUIUtil getInstance] showTipViewAutoDis:@"系统未知错误"];
            }else if ([[body objectForKey:@"ErrorCode"] isEqualToString:@"0002"]){
                [[YMUIUtil getInstance] showTipViewAutoDis:@"应用系统访问标识不存在"];
            }else if ([[body objectForKey:@"ErrorCode"] isEqualToString:@"0003"]){
                [[YMUIUtil getInstance] showTipViewAutoDis:@"应用系统访问密码不正确"];
            }else if ([[body objectForKey:@"ErrorCode"] isEqualToString:@"0004"]){
                [[YMUIUtil getInstance] showTipViewAutoDis:@"访问用户不存在"];
            }else if ([[body objectForKey:@"ErrorCode"] intValue] == 0005){
                [[YMUIUtil getInstance] showTipViewAutoDis:@"访问用户密码不正确"];
            }else if ([[body objectForKey:@"ErrorCode"] isEqualToString:@"0006"]){
                [[YMUIUtil getInstance] showTipViewAutoDis:@"访问用户已锁定"];
            }else if ([[body objectForKey:@"ErrorCode"] isEqualToString:@"0007"]){
                [[YMUIUtil getInstance] showTipViewAutoDis:@"不支持该访问接口服务"];
            }else if ([[body objectForKey:@"ErrorCode"] isEqualToString:@"0008"]){
                [[YMUIUtil getInstance] showTipViewAutoDis:@"访问用户没有操作权限"];
            }else if ([[body objectForKey:@"ErrorCode"] isEqualToString:@"0009"]){
                [[YMUIUtil getInstance] showTipViewAutoDis:@"传入参数不正确"];
            }else if ([[body objectForKey:@"ErrorCode"] isEqualToString:@"0010"]){
                [[YMUIUtil getInstance] showTipViewAutoDis:@"未授权访问"];
            }
            
        }
    }else{
        if ([[YMNetDataUtil getSuccessState:body] intValue] == 1){
            [[UserInfo getInstance] saveUserPrivilege:body];
            
            [self dismissViewControllerAnimated:YES completion:^{}];
            [kAPPDelegate rootView];
        }else{
            [[YMUIUtil getInstance] showTipViewAutoDis:@"获取权限控制失败"];
        }
    }
    
}


#pragma

#pragma mark **************************** UITextField Delegate ************************
- (void)textFieldDidBeginEditing:(UITextField *)textField{
    _currentField = textField;
}


- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
}

@end
